<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <div slot="header" class="clearfix">

          <el-button type="primary" icon="el-icon-plus" @click="showDialog">新增角色</el-button>
        </div>
        <template>
          <el-table
            :data="tableData"
            style="width: 100%"
          >
            <el-table-column
              prop="id"
              label="序号"
              width="180"
            />
            <el-table-column
              prop="name"
              label="角色名称"
              width="240"
            />
            <el-table-column
              prop="description"
              label="描述"
            />
            <el-table-column
              prop="address"
              label="操作"
            ><template slot-scope="scope">
              <el-button size="small">分配权限</el-button>
              <el-button size="small" @click="showUpdate(scope.row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click.native="delRole(scope.row.id)">删除</el-button>
            </template>
            </el-table-column>
          </el-table>
        </template>
        <!-- 分页 -->
        <div class="block" type="flex" justify="center" align="middle" style="height: 60px">

          <el-pagination
            layout="prev, pager, next"
            :current-page="pages.page"
            :page-size="pages.pagesize"
            :total="total"
            @current-change="changepage"
          />
        </div>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog title="新增角色" :visible="roleDialogVisible" @close="closeRoleDialog">
        <!-- 表单内容 -->
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-button size="small" @click="closeRoleDialog">取消</el-button>
          <el-button size="small" type="primary" @click="addRole">确定</el-button>
        </el-row>
      </el-dialog>
      <!-- 修改角色 -->
      <el-dialog title="修改角色" :visible="updataDialogVisible" @close="closeUpdDialog">
        <!-- 表单内容 -->
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-button size="small" @click="closeUpdDialog">取消</el-button>
          <el-button size="small" type="primary" @click="updateRole">确定</el-button>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { fetchRoleList, delRoleList, addRoleList, getRoledeatial, updateRole } from '@/api/setting'
export default {
  data() {
    return {
      tableData: [],
      pages: {
        page: 1,
        pagesize: 3
      },
      total: 0,
      roleDialogVisible: false,
      updataDialogVisible: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows, total } = await fetchRoleList(this.pages)
      this.tableData = rows
      this.total = total
    },
    changepage(page) {
      this.pages.page = page
      this.getRoleList()
    },
    // 删除
    delRole(id) {
      console.log(id)
      this.$confirm('确认要删除吗').then(async() => {
        await delRoleList(id)
        this.$message.success('删除成功')
        this.getRoleList()
      })
    },
    showDialog() {
      this.roleDialogVisible = true
    },
    // 关闭弹框
    closeRoleDialog() {
      this.roleDialogVisible = false
      this.resetRoleForm()
      this.$refs.roleForm.resetFields()
    },
    resetRoleForm() {
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 添加角色
    addRole() {
      this.$refs.roleForm.validate(async valid => {
        if (valid) {
          await addRoleList(this.roleForm)
          this.$message.success('新增角色成功')
          this.getRoleList()
          this.roleDialogVisible = false
          this.resetRoleForm()
        }
      })
    },
    async showUpdate(id) {
      this.updataDialogVisible = true
      const res = await getRoledeatial(id)
      this.roleForm = res
    },
    closeUpdDialog() {
      this.updataDialogVisible = false
      this.resetRoleForm()
      this.$refs.roleForm.resetFields()
    },
    updateRole() {
      this.$refs.roleForm.validate(async valid => {
        if (valid) {
          await updateRole(this.roleForm)
          this.$message.success('修改角色成功')
          this.getRoleList()
          this.updataDialogVisible = false
          this.resetRoleForm()
        }
      })
    }
  }
}
</script>

<style>
</style>
